package com.syni.mdd.sjb.common.datatreaure.service.impl.databank;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.syni.dto.synimerchants.ApiSyniMerchantsAccountRqDto;
import com.syni.mdd.sjb.common.component.mybatis.common.entity.DxshAppUserInfo;
import com.syni.mdd.sjb.common.component.mybatis.dxshapp.entity.DankMerchantsDefBusiness;
import com.syni.mdd.sjb.common.component.mybatis.dxshapp.entity.DxshAppNews;
import com.syni.mdd.sjb.common.component.mybatis.dxshapp.entity.DxshAppUserChannel;
import com.syni.mdd.sjb.common.component.mybatis.dxshapp.entity.customBo.DxshAppNewsBo;
import com.syni.mdd.sjb.common.component.utils.common.thread.ThreadPoolUtils;
import com.syni.mdd.sjb.common.component.utils.common.utils.*;
import com.syni.mdd.sjb.common.component.utils.feign.dto.mddAll.apiBusiness.GetUserClaimBusinessRpDto;
import com.syni.mdd.sjb.common.component.utils.feign.dto.mddUser.LoginDxshAppUserDto;
import com.syni.mdd.sjb.common.component.utils.feign.dto.mddUser.RegisterDxshAppUserDto;
import com.syni.mdd.sjb.common.component.utils.feign.service.mdd.commonuser.UserMerchantsRelationRestService;
import com.syni.mdd.sjb.common.component.utils.feign.service.mdd.commonuser.UserRestService;
import com.syni.mdd.sjb.common.component.utils.feign.service.mddAll.BusinessAuthRestService;
import com.syni.mdd.sjb.common.component.utils.feign.service.mddAll.BusinessRestService;
import com.syni.mdd.sjb.common.component.utils.feign.service.synimerchants.SyniMerchantsAccountRestService;
import com.syni.mdd.sjb.common.datatreaure.constants.Constants;
import com.syni.mdd.sjb.common.datatreaure.constants.ResultCode;
import com.syni.mdd.sjb.common.datatreaure.constants.ResultMsg;
import com.syni.mdd.sjb.common.datatreaure.controller.protect.dataTreasure.business.BankBusinessController;
import com.syni.mdd.sjb.common.datatreaure.entity.bo.databankUser.MerchantsUserInfoBo;
import com.syni.mdd.sjb.common.datatreaure.entity.vo.rp.dataBankBusiness.GetBusinessRpVo;
import com.syni.mdd.sjb.common.datatreaure.entity.vo.rp.dataBankBusiness.NewDetailParamsRpVo;
import com.syni.mdd.sjb.common.datatreaure.entity.vo.rp.databankUser.GetMerchantsNoReadNewsRpVo;
import com.syni.mdd.sjb.common.datatreaure.entity.vo.rp.databankUser.RegisterRpVo;
import com.syni.mdd.sjb.common.datatreaure.entity.vo.rq.databank.DataBankBusinessVo;
import com.syni.mdd.sjb.common.datatreaure.entity.vo.rq.databank.DataBankLoginVo;
import com.syni.mdd.sjb.common.datatreaure.entity.vo.rq.user.ForgetVo;
import com.syni.mdd.sjb.common.datatreaure.feign.commonTool.CommonUserService;
import com.syni.mdd.sjb.common.datatreaure.feign.commonTool.dto.UserBindWxDto;
import com.syni.mdd.sjb.common.datatreaure.service.*;
import com.syni.mdd.sjb.common.datatreaure.service.databank.DataBankBusinessService;
import com.syni.mdd.sjb.common.datatreaure.service.databank.DataBankCommonService;
import com.syni.mdd.sjb.common.datatreaure.service.databank.DataBankUserService;
import com.syni.mdd.sjb.common.datatreaure.utils.VerificationCodeUtils;
import io.seata.spring.annotation.GlobalTransactional;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Log4j2
public class DataBankUserServiceImpl implements DataBankUserService {

    @Autowired
    UserRestService userRestService;

    @Autowired
    CommonUserService commonUserService;

    @Autowired
    DxshAppUserChannelService dxshAppUserChannelService;

    @Autowired
    SyniMerchantsAccountRestService syniMerchantsAccountRestService;

    @Autowired
    UserMerchantsRelationRestService userMerchantsRelationRestService;

    @Autowired
    DankMerchantsDefBusinessService dankMerchantsDefBusinessService;

    @Autowired
    DxshAppNewsService dxshAppNewsService;

    @Autowired
    DxshAppIndustryChooseService dxshAppIndustryChooseService;

    @Autowired
    DxshAppUserSmsService dxshAppUserSmsService;

    @Autowired
    BusinessAuthRestService businessAuthRestService;

    @Autowired
    BusinessRestService businessRestService;

    @Autowired
    DxshAppIndustryService dxshAppIndustryService;

    @Autowired
    UserService userService;

    @Autowired
    DataBankBusinessService dataBankBusinessService;

    @Autowired
    DataBankCommonService dataBankCommonService;

    @Autowired
    DxshAppUserInfoService dxshAppUserInfoService;

    @Override
    @GlobalTransactional(rollbackFor = Exception.class)
    public Map<String, Object> register(DataBankLoginVo dataBankLoginVo) {
        RequestHeaderData requestHeaderData = new RequestHeaderData();
        //验证码是否正确
        boolean ret = VerificationCodeUtils.checkVerificationCode(dataBankLoginVo.getPhone(),dataBankLoginVo.getVerificationCode(), Constants.REG_CODE_TYPE);
        if(!ret){
            return ResultMapHelper.result(ResultCode.CODE_ERROR_100, ResultMsg.MSG_ACCOUNT_VER_CODE_ERROR);
        }
        //再去用户服务获取信息
        RpcResponseVo rpcResponseVo = userRestService.registerDataBankUser(dataBankLoginVo.getPhone(),
                dataBankLoginVo.getPassword(),dataBankLoginVo.getThirdPartyChannel());
        if(!rpcResponseVo.isSuccess()){
            return ResultMapHelper.result(ResultCode.CODE_ERROR_100,rpcResponseVo.getMsg());
        }
        RegisterDxshAppUserDto registerDxshAppUserDto = rpcResponseVo.parseObject(RegisterDxshAppUserDto.class);
        //添加一份电享生活用户信息
        DxshAppUserInfo dxshAppUserInfo = dxshAppUserInfoService.syncDataTreasureUser(registerDxshAppUserDto);
        RegisterRpVo registerRpVo = new RegisterRpVo();
        registerRpVo.setToken(registerDxshAppUserDto.getToken());
        DankMerchantsDefBusiness dankMerchantsDefBusiness = dankMerchantsDefBusinessService.getChooseBusiness(registerDxshAppUserDto.getUserId());
        if(dankMerchantsDefBusiness != null && dankMerchantsDefBusiness.getBusinessId() != null){
            registerRpVo.setBusinessId(dankMerchantsDefBusiness.getBusinessId());
        }
        int count = 0;
        RpcResponseVo responseVo = businessRestService.getUserClaimBusiness(registerDxshAppUserDto.getUserId(),Constants.COMMON_FLAG_FALSE_INT);
        if(responseVo.isSuccess()){
            List<GetUserClaimBusinessRpDto> getUserClaimBusinessRpDtos = responseVo.parseArray(GetUserClaimBusinessRpDto.class);
            count = getUserClaimBusinessRpDtos.size();
            //单店铺默认选择行业
            if(count == 1) {
                if (BankBusinessController.industryTypeMap.containsKey(getUserClaimBusinessRpDtos.get(0).getTypeName())) {
                    dxshAppIndustryService.chooseIndustry(registerDxshAppUserDto.getUserId(), BankBusinessController.industryTypeMap.get(getUserClaimBusinessRpDtos.get(0).getTypeName()));
                }
                if (getUserClaimBusinessRpDtos.get(0).getBusinessId() != null) {
                    dataBankBusinessService.chooseDefaultBusiness(registerDxshAppUserDto.getUserId(), getUserClaimBusinessRpDtos.get(0).getBusinessId());
                }
            }
            if(registerRpVo.getBusinessId() == 0 && count == 1 && getUserClaimBusinessRpDtos.get(0).getBusinessId() != null){
                registerRpVo.setBusinessId(getUserClaimBusinessRpDtos.get(0).getBusinessId());
            }
        }
        if(!dxshAppIndustryChooseService.isUserChooseInDustry(registerDxshAppUserDto.getUserId())) {
            //只有一家店
            if (count == 0) {
                registerRpVo.setIsNewUser(Constants.COMMON_FLAG_TRUE_INT);
            } else if (count > 1) {
            }
        }
        if(count > 1){
            registerRpVo.setIsHaveMoreBusiness(Constants.COMMON_FLAG_TRUE_INT);
            //判断是否有默认
            if (dankMerchantsDefBusiness == null) {
                registerRpVo.setIsNewUser(Constants.GOTO_TURN_BUSINESS);
            }
        }
        //选择的店铺模型
        GetBusinessRpVo getBusinessRpVo = this.getBusinessModel(registerDxshAppUserDto.getUserId(),registerRpVo.getBusinessId());
        if(getBusinessRpVo != null){
            registerRpVo.setBusiness(getBusinessRpVo);
        }
        //初始化用户短信
        dxshAppUserSmsService.intoUserSms(registerDxshAppUserDto.getUserId());
        if(registerDxshAppUserDto.getChannel() != null){
            registerRpVo.setThirdPartyChannel(registerDxshAppUserDto.getChannel());
        }
        if(StringUtils.isNotBlank(dxshAppUserInfo.getUnionId())){
            registerRpVo.setUnionId(dxshAppUserInfo.getUnionId());
        }
        if(StringUtils.isNotBlank(dataBankLoginVo.getOpenId())
                && StringUtils.isNotBlank(dataBankLoginVo.getUnionId())
                && StringUtils.isNotBlank(dataBankLoginVo.getAccessToken())){
            UserBindWxDto userBindWxDto = new UserBindWxDto();
            userBindWxDto.setUnionId(dataBankLoginVo.getUnionId());
            userBindWxDto.setOpenId(dataBankLoginVo.getOpenId());
            userBindWxDto.setAccessToken(dataBankLoginVo.getAccessToken());
            userBindWxDto.setUserId(dxshAppUserInfo.getUserId());
            try {
                ThreadPoolUtils.getExecutorService().submit(()->{
                    commonUserService.appBindWx(requestHeaderData.getString("token"),userBindWxDto);
                });
            }catch (Exception e){
                log.error(e.getMessage(),e);
            }
        }
        return ResultMapHelper.success(registerRpVo,ResultMsg.HAND_SUCCESS);
    }

    @Override
    @GlobalTransactional(rollbackFor = Exception.class)
    public Map<String, Object> login(DataBankLoginVo dataBankLoginVo) {
        ApiSyniMerchantsAccountRqDto apiSyniMerchantsAccountRqDto = new ApiSyniMerchantsAccountRqDto();
        apiSyniMerchantsAccountRqDto.setLoginWay(dataBankLoginVo.getLoginWay());
        apiSyniMerchantsAccountRqDto.setPhone(dataBankLoginVo.getPhone());
        RegisterRpVo registerRpVo = new RegisterRpVo();
        registerRpVo.setLoginWay(dataBankLoginVo.getLoginWay());
        String loginBody = "";
        if(dataBankLoginVo.getLoginWay() == Constants.LOGIN_BY_CODE){
            //todo 校验验证码
            boolean ret = VerificationCodeUtils.checkVerificationCode(dataBankLoginVo.getPhone(),dataBankLoginVo.getVerificationCode(), Constants.LOGIN_CODE_TYPE);
            if(!ret){
                return ResultMapHelper.result(ResultCode.CODE_ERROR_100, ResultMsg.MSG_ACCOUNT_VER_CODE_ERROR);
            }
            loginBody = dataBankLoginVo.getVerificationCode();
        }else if(dataBankLoginVo.getLoginWay() == Constants.LOGIN_BY_PWD){
            //通过手机号登陆
            apiSyniMerchantsAccountRqDto.setPassword(dataBankLoginVo.getPassword());
            loginBody = dataBankLoginVo.getPassword();
        }else if(dataBankLoginVo.getLoginWay() == Constants.LOGIN_BY_WECHAT){
            //检查这个微信号是否已经存在了
            DxshAppUserInfo dxshAppUserInfo = dxshAppUserInfoService.getByUnionId(dataBankLoginVo.getUnionId());
            if(dxshAppUserInfo == null){
                return ResultMapHelper.success(registerRpVo,ResultMsg.HAND_SUCCESS);
            }
            registerRpVo.setIsBindPhone(Constants.COMMON_FLAG_TRUE_INT);
            //校验通过当成是验证码登录
            dataBankLoginVo.setLoginWay(Constants.LOGIN_BY_CODE);
            dataBankLoginVo.setPhone(dxshAppUserInfo.getPhone());
        }
        RpcResponseVo rpcResponseVo = userRestService.loginDataBankUserByChannel(dataBankLoginVo.getPhone(),
                dataBankLoginVo.getLoginWay(),loginBody,dataBankLoginVo.getThirdPartyChannel());
        if(!rpcResponseVo.isSuccess()) {
            return ResultMapHelper.result(ResultCode.CODE_ERROR_100, rpcResponseVo.getMsg());
        }
        LoginDxshAppUserDto loginDxshAppUserDto  = rpcResponseVo.parseObject(LoginDxshAppUserDto.class);
        //添加一份电享生活用户信息
        DxshAppUserInfo dxshAppUserInfo = dxshAppUserInfoService.syncDataTreasureUser(loginDxshAppUserDto);
        registerRpVo.setToken(loginDxshAppUserDto.getToken());
        DankMerchantsDefBusiness dankMerchantsDefBusiness = dankMerchantsDefBusinessService.getChooseBusiness(loginDxshAppUserDto.getUserId());
        if(dankMerchantsDefBusiness != null && dankMerchantsDefBusiness.getBusinessId() != null){
            registerRpVo.setBusinessId(dankMerchantsDefBusiness.getBusinessId());
        }
        //看是否认领过店铺
        int count = 0;
        RpcResponseVo responseVo = businessRestService.getUserClaimBusiness(loginDxshAppUserDto.getUserId(),Constants.COMMON_FLAG_FALSE_INT);
        if(responseVo.isSuccess()){
            List<GetUserClaimBusinessRpDto> getUserClaimBusinessRpDtos = responseVo.parseArray(GetUserClaimBusinessRpDto.class);
            count = getUserClaimBusinessRpDtos.size();
            //单店铺默认选择行业
            if(count == 1) {
                if (BankBusinessController.industryTypeMap.containsKey(getUserClaimBusinessRpDtos.get(0).getTypeName())) {
                    dxshAppIndustryService.chooseIndustry(loginDxshAppUserDto.getUserId(), BankBusinessController.industryTypeMap.get(getUserClaimBusinessRpDtos.get(0).getTypeName()));
                }
                if (getUserClaimBusinessRpDtos.get(0).getBusinessId() != null) {
                    dataBankBusinessService.chooseDefaultBusiness(loginDxshAppUserDto.getUserId(), getUserClaimBusinessRpDtos.get(0).getBusinessId());
                }
            }
            if(registerRpVo.getBusinessId() == 0 && count == 1 && getUserClaimBusinessRpDtos.get(0).getBusinessId() != null){
                registerRpVo.setBusinessId(getUserClaimBusinessRpDtos.get(0).getBusinessId());
            }
        }
        if(!dxshAppIndustryChooseService.isUserChooseInDustry(loginDxshAppUserDto.getUserId())) {
            //只有一家店
            if (count == 0) {
                registerRpVo.setIsNewUser(Constants.COMMON_FLAG_TRUE_INT);
            } else if (count > 1) {

            }
        }
        if(count > 1){
            registerRpVo.setIsHaveMoreBusiness(Constants.COMMON_FLAG_TRUE_INT);
            //判断是否有默认
            if (dankMerchantsDefBusiness == null) {
                registerRpVo.setIsNewUser(Constants.GOTO_TURN_BUSINESS);
            }
        }
        //查看是否是店员
        if(loginDxshAppUserDto.getIsShopAssistant() == Constants.COMMON_FLAG_TRUE_INT){
            registerRpVo.setIsNewUser(Constants.COMMON_FLAG_FALSE_INT);
            registerRpVo.setIsHaveMoreBusiness(Constants.COMMON_FLAG_FALSE_INT);
            registerRpVo.setBusinessId(loginDxshAppUserDto.getBelongsToBusinessId());
        }
        //选择的店铺模型
        GetBusinessRpVo getBusinessRpVo = this.getBusinessModel(loginDxshAppUserDto.getUserId(),registerRpVo.getBusinessId());
        if(getBusinessRpVo != null){
            registerRpVo.setBusiness(getBusinessRpVo);
        }
        //初始化用户短信
        dxshAppUserSmsService.intoUserSms(loginDxshAppUserDto.getUserId());
        if(loginDxshAppUserDto.getChannel() != null){
            registerRpVo.setThirdPartyChannel(loginDxshAppUserDto.getChannel());
        }
        if(StringUtils.isNotBlank(dxshAppUserInfo.getUnionId())){
            registerRpVo.setUnionId(dxshAppUserInfo.getUnionId());
        }
        return ResultMapHelper.success(registerRpVo,ResultMsg.HAND_SUCCESS);
    }

    private GetBusinessRpVo getBusinessModel(Integer userId,Integer businessId) {
        DataBankBusinessVo dataBankBusinessVo = new DataBankBusinessVo();
        dataBankBusinessVo.setUserId(userId);
        dataBankBusinessVo.setBusinessId(businessId);
        Map<String,Object> resultMap = dataBankBusinessService.getBusiness(dataBankBusinessVo);
        if(ResultMapHelper.isSuccess(resultMap)){
            Map<String,Object> data = (Map<String, Object>) resultMap.get("data");
            GetBusinessRpVo getBusinessRpVo = (GetBusinessRpVo) data.get("business");
            return getBusinessRpVo;
        }
        return null;
    }

    @Override
    public Map<String, Object> forget(DataBankLoginVo dataBankLoginVo) {
        ForgetVo forgetVo = new ForgetVo();
        forgetVo.setIdentifyCode(dataBankLoginVo.getVerificationCode());
        forgetVo.setPassword(dataBankLoginVo.getPassword());
        forgetVo.setPhone(dataBankLoginVo.getPhone());
        return userService.forget(forgetVo);
        //
//        if(!PatternMatchUtils.checkPhone(dataBankLoginVo.getPhone())){
//            return ResultMapHelper.result(ResultCode.CODE_ERROR_100,ResultMsg.PHONE_FORMAT_ERROR_MSG);
//        }
//        RpcResponseVo rpcResponseVo = syniMerchantsAccountRestService.getMerchantsByPhone(dataBankLoginVo.getPhone());
//        if(!rpcResponseVo.isSuccess()){
//            return ResultMapHelper.result(ResultCode.CODE_ERROR_100,rpcResponseVo.getMsg());
//        }
//        String accountId = rpcResponseVo.getDataElement("accountId").toString();
//        //校验验证码
//        boolean ret = VerificationCodeUtils.checkVerificationCode(dataBankLoginVo.getPhone(),dataBankLoginVo.getVerificationCode(), Constants.FORGET_CODE_TYPE);
//        if(!ret){
//            return ResultMapHelper.result(ResultCode.CODE_ERROR_100, ResultMsg.MSG_ACCOUNT_VER_CODE_ERROR);
//        }
//        //修改密码
//        ApiSyniMerchantsAccountRqDto apiSyniMerchantsAccountRqDto = new ApiSyniMerchantsAccountRqDto();
//        apiSyniMerchantsAccountRqDto.setMerchantsAccountId(accountId);
//        apiSyniMerchantsAccountRqDto.setPassword(WebUtils.MD5(dataBankLoginVo.getPassword()));
//        RpcResponseVo rpcResponseVo1 = syniMerchantsAccountRestService.updateMerchantsAccountByAccountId(apiSyniMerchantsAccountRqDto);
//        if(!rpcResponseVo1.isSuccess()){
//            return ResultMapHelper.result(ResultCode.CODE_ERROR_100,rpcResponseVo1.getMsg());
//        }
//        return ResultMapHelper.success(null,ResultMsg.UPDATE_SUCCESS);
    }

    @Override
    public MerchantsUserInfoBo getMerchantsUserInfo(Integer userId) {
//        RpcResponseVo rpcResponseVo = userRestService.getUserInfo(merchantsUserId);
//        if(!rpcResponseVo.isSuccess()){
//            return new MerchantsUserInfoBo();
//        }
        DxshAppUserInfo dxshAppUserInfo = dxshAppUserInfoService.getByUserId(userId);
        if(dxshAppUserInfo == null){
            return new MerchantsUserInfoBo();
        }
        MerchantsUserInfoBo merchantsUserInfoBo = BeanUtils.copyProperties(dxshAppUserInfo,MerchantsUserInfoBo.class);
        merchantsUserInfoBo.setNickname(merchantsUserInfoBo.getSurName() + merchantsUserInfoBo.getName());
        if(StringUtils.isNotBlank(dxshAppUserInfo.getUnionId())){
            merchantsUserInfoBo.setIsBindWechat(Constants.COMMON_FLAG_TRUE_INT);
        }
        return merchantsUserInfoBo;
    }

    @Override
    public Map<String, Object> getMerchantsNoReadNews(DataBankLoginVo dataBankLoginVo) {
        //获取店铺id
//        Integer businessId = dankMerchantsDefBusinessService.getChooseBusinessId(dataBankLoginVo.getUserId());
        GetMerchantsNoReadNewsRpVo getMerchantsNoReadNewsRpVo = new GetMerchantsNoReadNewsRpVo();
//        if(businessId == null){
//            return ResultMapHelper.success(getMerchantsNoReadNewsRpVo,ResultMsg.MSG_GET_SUCCESS);
//        }
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq(DxshAppNews.USER_ID, dataBankLoginVo.getUserId());
        queryWrapper.eq(DxshAppNews.IS_CLEAR, Constants.COMMON_FLAG_FALSE_INT);
        queryWrapper.eq(DxshAppNews.IS_WATCH, Constants.COMMON_FLAG_FALSE_INT);
        Integer channelId = dataBankLoginVo.getChannelId();
        boolean isSearchByChannelId = false;
        if(channelId != null){
            DxshAppUserChannel dxshAppUserChannel = dxshAppUserChannelService.getById(channelId);
            if(dxshAppUserChannel == null){
                return ResultMapHelper.success(getMerchantsNoReadNewsRpVo,ResultMsg.MSG_GET_SUCCESS);
            }
            if(dxshAppUserChannel.getChannel() == DxshAppUserChannelService.CHANNEL_MINAPP){
                queryWrapper.eq(DxshAppNews.STORE_ID,dxshAppUserChannel.getStoreId());
                queryWrapper.eq(DxshAppNews.TYPE,Constants.APP_NEWS_MINAPP);
            }else if(dxshAppUserChannel.getChannel() == DxshAppUserChannelService.CHANNEL_MDD){
                queryWrapper.eq(DxshAppNews.BUSINESS_ID,dxshAppUserChannel.getBusinessId());
                queryWrapper.eq(DxshAppNews.TYPE,Constants.APP_NEWS_MIDONGDONG);
            }
            isSearchByChannelId = true;
        }
        Page<DxshAppNews> page = new Page<>(dataBankLoginVo.getPageNum(),dataBankLoginVo.getPageSize());
        if(!isSearchByChannelId) {
            List<DxshAppNewsBo> dxshAppNewsBos = dxshAppNewsService.countByMessageType(queryWrapper);
            Map<Integer,Integer> dxshAppNewsBoMap = new HashMap<>();
            if(CollectionUtils.isNotEmpty(dxshAppNewsBos)){
                for(DxshAppNewsBo dxshAppNewsBo : dxshAppNewsBos){
                    dxshAppNewsBoMap.put(dxshAppNewsBo.getMessageType(),dxshAppNewsBo.getNum());
                }
            }
            if(dxshAppNewsBoMap.containsKey(Constants.DXSH_APP_NEW_ORDER_STATUS)){
                getMerchantsNoReadNewsRpVo.setOrderStatusUnreadNum(dxshAppNewsBoMap.get(Constants.DXSH_APP_NEW_ORDER_STATUS));
            }
            if(dxshAppNewsBoMap.containsKey(Constants.DXSH_APP_NEW_MARKET_REALITY)){
                getMerchantsNoReadNewsRpVo.setMarketingStatusUnread(dxshAppNewsBoMap.get(Constants.DXSH_APP_NEW_MARKET_REALITY));
            }
            if(dxshAppNewsBoMap.containsKey(Constants.DXSH_APP_NEW_ORTHER)){
                getMerchantsNoReadNewsRpVo.setOtherNotices(dxshAppNewsBoMap.get(Constants.DXSH_APP_NEW_ORTHER));
            }
            queryWrapper.eq(DxshAppNews.MESSAGE_TYPE,dataBankLoginVo.getMessageType());
        }
        queryWrapper.orderByDesc(DxshAppNews.NEW_TIME);
        Page<DxshAppNews> dxshAppNewsPage = dxshAppNewsService.page(page,queryWrapper);
        List<DxshAppNews> dxshAppNews = dxshAppNewsPage.getRecords();
        if(dataBankLoginVo.getPageNum().longValue() == dxshAppNewsPage.getPages()){
            getMerchantsNoReadNewsRpVo.setIsLastPage(Constants.COMMON_FLAG_TRUE_INT);
        }
        if(CollectionUtils.isNotEmpty(dxshAppNews)){
            //getMerchantsNoReadNewsRpVo.setNoReadNews(BeanUtils.copyList(dxshAppNews, GetMerchantsNoReadNewsRpVo.GetMerchantsNoReadNewsRpDxshAppNews.class));
            for(DxshAppNews dx : dxshAppNews){
                GetMerchantsNoReadNewsRpVo.GetMerchantsNoReadNewsRpDxshAppNews getMerchantsNoReadNewsRpDxshAppNews = new GetMerchantsNoReadNewsRpVo.GetMerchantsNoReadNewsRpDxshAppNews();
                getMerchantsNoReadNewsRpDxshAppNews.setId(dx.getId());
                getMerchantsNoReadNewsRpDxshAppNews.setMainTitle(dx.getMainTitle());
                //数据兼容旧版本
                getMerchantsNoReadNewsRpDxshAppNews.setTitle(dx.getDescribes());
                getMerchantsNoReadNewsRpDxshAppNews.setDescribes(dx.getTitle());
                getMerchantsNoReadNewsRpDxshAppNews.setNewTime(dx.getNewTime());
                getMerchantsNoReadNewsRpDxshAppNews.setMessageType(dx.getMessageType());
                getMerchantsNoReadNewsRpDxshAppNews.setMessageSecondType(dx.getMessageSecondType());
                NewDetailParamsRpVo newDetailParamsRpVo = new NewDetailParamsRpVo();
                dataBankCommonService.handleNesDetail(dx,newDetailParamsRpVo);
                getMerchantsNoReadNewsRpDxshAppNews.setGlobalMessageType(newDetailParamsRpVo.getGlobalMessageType());
                getMerchantsNoReadNewsRpVo.getNews().add(getMerchantsNoReadNewsRpDxshAppNews);
            }
        }
        return ResultMapHelper.success(getMerchantsNoReadNewsRpVo,ResultMsg.MSG_GET_SUCCESS);
    }

    @Override
    public Map<String, Object> getMerchantsNews(DataBankLoginVo dataBankLoginVo) {
        GetMerchantsNoReadNewsRpVo getMerchantsNoReadNewsRpVo = new GetMerchantsNoReadNewsRpVo();
//        Integer businessId = dankMerchantsDefBusinessService.getChooseBusinessId(dataBankLoginVo.getUserId());
//        if(businessId == null){
//            return ResultMapHelper.success(getMerchantsNoReadNewsRpVo,ResultMsg.MSG_GET_SUCCESS);
//        }
        QueryWrapper queryWrapper = new QueryWrapper();
        Integer channelId = dataBankLoginVo.getChannelId();
        boolean isSearchByChannelId = false;
        if(channelId != null){
            queryWrapper.eq(DxshAppNews.USER_ID,dataBankLoginVo.getUserId());
            queryWrapper.eq(DxshAppNews.IS_CLEAR,Constants.COMMON_FLAG_FALSE_INT);
            DxshAppUserChannel dxshAppUserChannel = dxshAppUserChannelService.getById(channelId);
            if(dxshAppUserChannel == null){
                return ResultMapHelper.success(getMerchantsNoReadNewsRpVo,ResultMsg.MSG_GET_SUCCESS);
            }
            if(dxshAppUserChannel.getChannel() == DxshAppUserChannelService.CHANNEL_MINAPP){
                queryWrapper.eq(DxshAppNews.STORE_ID,dxshAppUserChannel.getStoreId());
                queryWrapper.eq(DxshAppNews.TYPE,Constants.APP_NEWS_MINAPP);
            }else if(dxshAppUserChannel.getChannel() == DxshAppUserChannelService.CHANNEL_MDD){
                queryWrapper.eq(DxshAppNews.BUSINESS_ID,dxshAppUserChannel.getBusinessId());
                queryWrapper.eq(DxshAppNews.TYPE,Constants.APP_NEWS_MIDONGDONG);
            }
            isSearchByChannelId = true;
        }
        Page<DxshAppNews> page = new Page<>(dataBankLoginVo.getPageNum(),dataBankLoginVo.getPageSize());
        if(!isSearchByChannelId) {
            QueryWrapper countQueryWrapper = new QueryWrapper();
            countQueryWrapper.eq(DxshAppNews.USER_ID, dataBankLoginVo.getUserId());
            countQueryWrapper.eq(DxshAppNews.IS_CLEAR, Constants.COMMON_FLAG_FALSE_INT);
            countQueryWrapper.eq(DxshAppNews.IS_WATCH, Constants.COMMON_FLAG_FALSE_INT);
            List<DxshAppNewsBo> dxshAppNewsBos = dxshAppNewsService.countByMessageType(countQueryWrapper);
            Map<Integer, Integer> dxshAppNewsBoMap = new HashMap<>();
            if (CollectionUtils.isNotEmpty(dxshAppNewsBos)) {
                for (DxshAppNewsBo dxshAppNewsBo : dxshAppNewsBos) {
                    dxshAppNewsBoMap.put(dxshAppNewsBo.getMessageType(), dxshAppNewsBo.getNum());
                }
            }
            if (dxshAppNewsBoMap.containsKey(Constants.DXSH_APP_NEW_ORDER_STATUS)) {
                getMerchantsNoReadNewsRpVo.setOrderStatusUnreadNum(dxshAppNewsBoMap.get(Constants.DXSH_APP_NEW_ORDER_STATUS));
            }
            if (dxshAppNewsBoMap.containsKey(Constants.DXSH_APP_NEW_MARKET_REALITY)) {
                getMerchantsNoReadNewsRpVo.setMarketingStatusUnread(dxshAppNewsBoMap.get(Constants.DXSH_APP_NEW_MARKET_REALITY));
            }
            if (dxshAppNewsBoMap.containsKey(Constants.DXSH_APP_NEW_ORTHER)) {
                getMerchantsNoReadNewsRpVo.setOtherNotices(dxshAppNewsBoMap.get(Constants.DXSH_APP_NEW_ORTHER));
            }
            queryWrapper.eq(DxshAppNews.USER_ID,dataBankLoginVo.getUserId());
            queryWrapper.eq(DxshAppNews.IS_CLEAR,Constants.COMMON_FLAG_FALSE_INT);
            queryWrapper.eq(DxshAppNews.MESSAGE_TYPE,dataBankLoginVo.getMessageType());
        }
        queryWrapper.orderByDesc(DxshAppNews.NEW_TIME);
        Page<DxshAppNews> dxshAppNewsPage = dxshAppNewsService.page(page,queryWrapper);
        List<DxshAppNews> dxshAppNews = dxshAppNewsPage.getRecords();
        if(dataBankLoginVo.getPageNum().longValue() == dxshAppNewsPage.getPages()){
            getMerchantsNoReadNewsRpVo.setIsLastPage(Constants.COMMON_FLAG_TRUE_INT);
        }
        if(CollectionUtils.isNotEmpty(dxshAppNews)){
            //getMerchantsNoReadNewsRpVo.setNoReadNews(BeanUtils.copyList(dxshAppNews, GetMerchantsNoReadNewsRpVo.GetMerchantsNoReadNewsRpDxshAppNews.class));
            for(DxshAppNews dx : dxshAppNews){
                GetMerchantsNoReadNewsRpVo.GetMerchantsNoReadNewsRpDxshAppNews getMerchantsNoReadNewsRpDxshAppNews = new GetMerchantsNoReadNewsRpVo.GetMerchantsNoReadNewsRpDxshAppNews();
                getMerchantsNoReadNewsRpDxshAppNews.setId(dx.getId());
                getMerchantsNoReadNewsRpDxshAppNews.setMainTitle(dx.getMainTitle());
                //数据兼容旧版本
                getMerchantsNoReadNewsRpDxshAppNews.setTitle(dx.getDescribes());
                getMerchantsNoReadNewsRpDxshAppNews.setDescribes(dx.getTitle());
                getMerchantsNoReadNewsRpDxshAppNews.setNewTime(dx.getNewTime());
                getMerchantsNoReadNewsRpDxshAppNews.setMessageType(dx.getMessageType());
                getMerchantsNoReadNewsRpDxshAppNews.setMessageSecondType(dx.getMessageSecondType());
                getMerchantsNoReadNewsRpDxshAppNews.setIsWatch(dx.getIsWatch());
                NewDetailParamsRpVo newDetailParamsRpVo = new NewDetailParamsRpVo();
                dataBankCommonService.handleNesDetail(dx,newDetailParamsRpVo);
                getMerchantsNoReadNewsRpDxshAppNews.setGlobalMessageType(newDetailParamsRpVo.getGlobalMessageType());
                getMerchantsNoReadNewsRpVo.getNews().add(getMerchantsNoReadNewsRpDxshAppNews);
            }
        }
        return ResultMapHelper.success(getMerchantsNoReadNewsRpVo,ResultMsg.MSG_GET_SUCCESS);
    }

    @Override
    public Map<String, Object> getMerchantsNewsByChannelId(DataBankLoginVo dataBankLoginVo) {
        GetMerchantsNoReadNewsRpVo getMerchantsNoReadNewsRpVo = new GetMerchantsNoReadNewsRpVo();
        Integer channelId = dataBankLoginVo.getChannelId();
        DxshAppUserChannel dxshAppUserChannel = dxshAppUserChannelService.getById(channelId);
        if(dxshAppUserChannel == null){
            return ResultMapHelper.success(getMerchantsNoReadNewsRpVo,ResultMsg.MSG_GET_SUCCESS);
        }
        Page<DxshAppNews> page = new Page<>(dataBankLoginVo.getPageNum(),dataBankLoginVo.getPageSize());
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq(DxshAppNews.IS_CLEAR,Constants.COMMON_FLAG_FALSE_INT);
        if(dxshAppUserChannel.getChannel() == DxshAppUserChannelService.CHANNEL_MINAPP){
            queryWrapper.eq(DxshAppNews.STORE_ID,dxshAppUserChannel.getStoreId());
        }else if(dxshAppUserChannel.getChannel() == DxshAppUserChannelService.CHANNEL_MDD){
            queryWrapper.eq(DxshAppNews.BUSINESS_ID,dxshAppUserChannel.getBusinessId());
        }
        queryWrapper.orderByDesc(DxshAppNews.NEW_TIME);
        Page<DxshAppNews> dxshAppNewsPage = dxshAppNewsService.page(page,queryWrapper);
        List<DxshAppNews> dxshAppNews = dxshAppNewsPage.getRecords();
        if(dataBankLoginVo.getPageNum().longValue() == dxshAppNewsPage.getPages()){
            getMerchantsNoReadNewsRpVo.setIsLastPage(Constants.COMMON_FLAG_TRUE_INT);
        }
        if(CollectionUtils.isNotEmpty(dxshAppNews)){
            for(DxshAppNews dx : dxshAppNews){
                GetMerchantsNoReadNewsRpVo.GetMerchantsNoReadNewsRpDxshAppNews getMerchantsNoReadNewsRpDxshAppNews = new GetMerchantsNoReadNewsRpVo.GetMerchantsNoReadNewsRpDxshAppNews();
                getMerchantsNoReadNewsRpDxshAppNews.setId(dx.getId());
                getMerchantsNoReadNewsRpDxshAppNews.setMainTitle(dx.getMainTitle());
                //数据兼容旧版本
                getMerchantsNoReadNewsRpDxshAppNews.setTitle(dx.getDescribes());
                getMerchantsNoReadNewsRpDxshAppNews.setDescribes(dx.getTitle());
                getMerchantsNoReadNewsRpDxshAppNews.setNewTime(dx.getNewTime());
                getMerchantsNoReadNewsRpDxshAppNews.setMessageType(dx.getMessageType());
                getMerchantsNoReadNewsRpDxshAppNews.setMessageSecondType(dx.getMessageSecondType());
                getMerchantsNoReadNewsRpDxshAppNews.setIsWatch(dx.getIsWatch());
                NewDetailParamsRpVo newDetailParamsRpVo = new NewDetailParamsRpVo();
                dataBankCommonService.handleNesDetail(dx,newDetailParamsRpVo);
                getMerchantsNoReadNewsRpDxshAppNews.setGlobalMessageType(newDetailParamsRpVo.getGlobalMessageType());
                getMerchantsNoReadNewsRpVo.getNews().add(getMerchantsNoReadNewsRpDxshAppNews);
            }
        }
        return ResultMapHelper.success(getMerchantsNoReadNewsRpVo,ResultMsg.MSG_GET_SUCCESS);
    }

    public Map<String, Object> getMerchantsAlreadyReadNews(DataBankLoginVo dataBankLoginVo) {
        GetMerchantsNoReadNewsRpVo getMerchantsNoReadNewsRpVo = new GetMerchantsNoReadNewsRpVo();
//        Integer businessId = dankMerchantsDefBusinessService.getChooseBusinessId(dataBankLoginVo.getUserId());
//        if(businessId == null){
//            return ResultMapHelper.success(getMerchantsNoReadNewsRpVo,ResultMsg.MSG_GET_SUCCESS);
//        }
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq(DxshAppNews.USER_ID, dataBankLoginVo.getUserId());
        queryWrapper.eq(DxshAppNews.IS_CLEAR, Constants.COMMON_FLAG_FALSE_INT);
        queryWrapper.eq(DxshAppNews.IS_WATCH, Constants.COMMON_FLAG_TRUE_INT);
        Integer channelId = dataBankLoginVo.getChannelId();
        boolean isSearchByChannelId = false;
        if(channelId != null){
            DxshAppUserChannel dxshAppUserChannel = dxshAppUserChannelService.getById(channelId);
            if(dxshAppUserChannel == null){
                return ResultMapHelper.success(getMerchantsNoReadNewsRpVo,ResultMsg.MSG_GET_SUCCESS);
            }
            if(dxshAppUserChannel.getChannel() == DxshAppUserChannelService.CHANNEL_MINAPP){
                queryWrapper.eq(DxshAppNews.STORE_ID,dxshAppUserChannel.getStoreId());
            }else if(dxshAppUserChannel.getChannel() == DxshAppUserChannelService.CHANNEL_MDD){
                queryWrapper.eq(DxshAppNews.BUSINESS_ID,dxshAppUserChannel.getBusinessId());
            }
            isSearchByChannelId = true;
        }
        Page<DxshAppNews> page = new Page<>(dataBankLoginVo.getPageNum(),dataBankLoginVo.getPageSize());
        if(!isSearchByChannelId) {
            queryWrapper.eq(DxshAppNews.MESSAGE_TYPE, dataBankLoginVo.getMessageType());
        }
        queryWrapper.orderByDesc(DxshAppNews.NEW_TIME);
        Page<DxshAppNews> dxshAppNewsPage = dxshAppNewsService.page(page,queryWrapper);
        List<DxshAppNews> dxshAppNews = dxshAppNewsPage.getRecords();
        if(dataBankLoginVo.getPageNum().longValue() == dxshAppNewsPage.getPages()){
            getMerchantsNoReadNewsRpVo.setIsLastPage(Constants.COMMON_FLAG_TRUE_INT);
        }
        if(CollectionUtils.isNotEmpty(dxshAppNews)){
            for(DxshAppNews dx : dxshAppNews){
                GetMerchantsNoReadNewsRpVo.GetMerchantsNoReadNewsRpDxshAppNews getMerchantsNoReadNewsRpDxshAppNews = new GetMerchantsNoReadNewsRpVo.GetMerchantsNoReadNewsRpDxshAppNews();
                getMerchantsNoReadNewsRpDxshAppNews.setId(dx.getId());
                getMerchantsNoReadNewsRpDxshAppNews.setMainTitle(dx.getMainTitle());
                //数据兼容旧版本
                getMerchantsNoReadNewsRpDxshAppNews.setTitle(dx.getDescribes());
                getMerchantsNoReadNewsRpDxshAppNews.setDescribes(dx.getTitle());
                getMerchantsNoReadNewsRpDxshAppNews.setNewTime(dx.getNewTime());
                getMerchantsNoReadNewsRpDxshAppNews.setMessageType(dx.getMessageType());
                getMerchantsNoReadNewsRpDxshAppNews.setMessageSecondType(dx.getMessageSecondType());
                NewDetailParamsRpVo newDetailParamsRpVo = new NewDetailParamsRpVo();
                dataBankCommonService.handleNesDetail(dx,newDetailParamsRpVo);
                getMerchantsNoReadNewsRpDxshAppNews.setGlobalMessageType(newDetailParamsRpVo.getGlobalMessageType());
                getMerchantsNoReadNewsRpVo.getNews().add(getMerchantsNoReadNewsRpDxshAppNews);
            }
        }
        return ResultMapHelper.success(getMerchantsNoReadNewsRpVo,ResultMsg.MSG_GET_SUCCESS);
    }

    @Override
    public void rightSlip(List<Integer> newId) {
        if(newId.size() > 0){
            QueryWrapper queryWrapper = new QueryWrapper();
            queryWrapper.in(DxshAppNews.ID,newId);
            List<DxshAppNews> dxshAppNews = dxshAppNewsService.list(queryWrapper);
            if(CollectionUtils.isNotEmpty(dxshAppNews)){
                for(DxshAppNews dx : dxshAppNews){
                    dx.setIsWatch(Constants.COMMON_FLAG_TRUE_INT);
                }
                dxshAppNewsService.updateBatchById(dxshAppNews);
            }
        }
    }

    @Override
    public void rightSlideDelete(List<Integer> newId) {
        if(newId.size() > 0){
            QueryWrapper queryWrapper = new QueryWrapper();
            queryWrapper.in(DxshAppNews.ID,newId);
            List<DxshAppNews> dxshAppNews = dxshAppNewsService.list(queryWrapper);
            if(CollectionUtils.isNotEmpty(dxshAppNews)){
                for(DxshAppNews dx : dxshAppNews){
                    dx.setIsClear(Constants.COMMON_FLAG_TRUE_INT);
                }
                dxshAppNewsService.updateBatchById(dxshAppNews);
            }
        }
    }

    @Override
    public void markAllAsRead(Integer userId, Integer messageType,Integer channelId) {
        boolean isSearchByChannelId = false;
        QueryWrapper queryWrapper = new QueryWrapper();
        if(channelId != null){
            DxshAppUserChannel dxshAppUserChannel = dxshAppUserChannelService.getById(channelId);
            if(dxshAppUserChannel == null){
                return;
            }
            if(dxshAppUserChannel.getChannel() == DxshAppUserChannelService.CHANNEL_MINAPP){
                queryWrapper.eq(DxshAppNews.STORE_ID,dxshAppUserChannel.getStoreId());
            }else if(dxshAppUserChannel.getChannel() == DxshAppUserChannelService.CHANNEL_MDD){
                queryWrapper.eq(DxshAppNews.BUSINESS_ID,dxshAppUserChannel.getBusinessId());
            }
            isSearchByChannelId = true;
        }
        if(!isSearchByChannelId) {
            queryWrapper.eq(DxshAppNews.USER_ID, userId);
            queryWrapper.eq(DxshAppNews.MESSAGE_TYPE, messageType);
        }
        List<DxshAppNews> dxshAppNews = dxshAppNewsService.list(queryWrapper);
        if(CollectionUtils.isNotEmpty(dxshAppNews)){
            for(DxshAppNews dx : dxshAppNews){
                dx.setIsWatch(Constants.COMMON_FLAG_TRUE_INT);
            }
            dxshAppNewsService.updateBatchById(dxshAppNews);
        }
    }


}
